﻿using System;
using System.Collections.Generic;
using System.Linq;
using Coolite.Ext.Web;
using Pap.Web.Modules.Reports.ViewModel;
using Pap.Web.Utils;

namespace Pap.Web.Modules.Reports.Views
{
    /// <summary>
    /// 
    /// </summary>
    public partial class UnionStatusReport : BasePage<UnionStatusReportViewModel>
    {
        /// <summary>
        /// Handles the Load event of the Page control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void Page_Load(object sender, EventArgs e)
        {
            this.Master.GenerateReport += Master_GenerateReport;
            if (!Ext.IsAjaxRequest)
            {
                FillReportParams();
            }
        }

        protected void Master_GenerateReport(object sender, EventArgs e)
        {
            var selectedProjects = lstProjects.SelectedItems.ToList().Select(x => x.Value.ToGuid()).ToList();
            DateTime? fromDate = dtFromDate.SelectedDate;
            DateTime? toDate = dtToDate.SelectedDate;

            this.strReport.DataSource =  this.ViewModel.GetUnionStatusReport(selectedProjects,fromDate,toDate);
            this.strReport.DataBind();
        }

        /// <summary>
        /// Handles the GetReport event of the Master control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        //void Master_GetReport(object sender, EventArgs e)
        //{
        //    var selectedProjects = lstProjects.SelectedItems.ToList().Select(x => x.Value).ToList();

        //    List<Guid> projects = new List<Guid>();

        //    foreach (string project in selectedProjects)
        //    {
        //        projects.Add(new Guid(project));
        //    }

        //    DateTime? FromDate = dtFromDate.SelectedDate;
        //    DateTime? ToDate = dtToDate.SelectedDate;

        //    ReportDataSource datasource = new ReportDataSource("dsUnionStatusReport",
        //           ViewModel.GetUnionStatusReport(projects, FromDate, ToDate).Tables[0]);

        //    rpvUnionStatusReport.LocalReport.DataSources.Clear();
        //    rpvUnionStatusReport.LocalReport.DataSources.Add(datasource);
        //    rpvUnionStatusReport.LocalReport.Refresh();
        //}

        /// <summary>
        /// Fills the report params.
        /// </summary>
        private void FillReportParams()
        {
            lstProjects.Items.Clear();
            strProjects.DataSource = this.ViewModel.GetAllProjects();
            strProjects.DataBind();
        }
    }
}
